package com.jservice.framework.utils;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.jfinal.log.Log;
import com.jservice.framework.dto.Setting;

/**
 * JpushUtils
 *
 * @Author cwledit
 * @DATE 2017/7/4.
 */
public class JpushUtils {
    private static final Log LOG = Log.getLog(JpushUtils.class);

    private static JPushClient jpushClient;

    public static JPushClient getInstance() {
        if (jpushClient == null) {
            // 同步块，线程安全的创建实例
            synchronized (JPushClient.class) {
                // 再次检查实例是否存在，如果不存在才真正的创建实例
                if (jpushClient == null) {
                    jpushClient = new JPushClient(Setting.getConfig("MASTER_SECRET"), Setting.getConfig("APP_KEY"), null, ClientConfig.getInstance());;
                }
            }
        }
        return jpushClient;
    }

    public static PushPayload iosPayLoad(String account, String message, int badge, String id) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(message)
                                .setBadge(badge)
                                .addExtra("id", id)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .build())
                .build();
    }

    public static PushPayload androidPayLoad(String account, String message, int badge, String id) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(message)
                                .addExtra("id", id)
                                .addExtra("badge",badge)
                                .build())
                        .build())
                .build();
    }

    public static void main(String[] args) {
        JPushClient jPushClient = JpushUtils.getInstance();
        PushPayload payload = JpushUtils.androidPayLoad("330327199303060953","IOS推送消息2",1,"1111");
        try {
            PushResult result = jPushClient.sendPush(payload);
            LOG.info("Got result - " + result);
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            LOG.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            LOG.error("Should review the error, and fix the request", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
    }
}
